package com.daiziru.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daiziru.entity.MCategory;
import com.daiziru.entity.MUserMessage;
import com.daiziru.mapper.MCategoryMapper;
import com.daiziru.service.MCategoryService;
import com.daiziru.service.MUserMessageService;
import com.daiziru.service.MUserService;
import com.daiziru.service.WebSocketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyan
 * @since 2022-12-07
 */
@Service
public class WebSocketServiceImpl implements WebSocketService {

    @Autowired
    MUserMessageService messageService;
    //websocket通讯模板
    @Autowired
    SimpMessagingTemplate messagingTemplate;
    @Override
    public void sendMessCountToUser(Long toUserId) {
        int count = messageService.count(new QueryWrapper<MUserMessage>()
                .eq("to_user_id", toUserId)
                .eq("status", "0")
        );

        // websocket通知 (/user/id/messCount)
        messagingTemplate.convertAndSendToUser(toUserId.toString(), "/messCount", count);
    }
}
